<template>
  <div>
    <el-card class="box-card">
      <el-form :inline="true" :model="userForm" class="demo-form-inline">
        <el-form-item label="用户姓名">
          <el-input v-model="userForm.name" placeholder="用户姓名"></el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="userForm.idNo" placeholder="身份证号"></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input
            v-model="userForm.telephone"
            placeholder="联系方式"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户状态">
          <el-select v-model="userForm.recFlag" placeholder="用户状态">
            <el-option label="有效" value="1"></el-option>
            <el-option label="无效" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="box-card" style="height: 65vh;overflow-y: scroll;">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="name" label="用户姓名" width="180">
        </el-table-column>
        <el-table-column prop="idNo" label="身份证号" width="180">
        </el-table-column>
        <el-table-column prop="sex" label="用户性别" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.sex == 1 ? "男" : "女" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="telephone" label="联系方式"> </el-table-column>
        <el-table-column prop="recFlag" label="用户状态">
          <template slot-scope="scope">
            <el-switch
              :value="scope.row.recFlag == 1"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeFlag(scope.row.userId)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="recFlag" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="goToDetail(scope.row.userId)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="tableSize"
      @current-change="pagechange">
    </el-pagination>
    <el-dialog title="用户详情" :visible.sync="showuserDetail">
      <div class="cDetail">用户id: {{ userDetail.userId }}</div>
      <div class="cDetail">身份证号:  {{ userDetail.idNo }}</div>
      <div class="cDetail">用户姓名: {{ userDetail.name }}</div>
      <div class="cDetail">用户昵称: {{ userDetail.nickName }}</div>
      <div class="cDetail">用户余额: ￥{{ userDetail.balance }}</div>
      <div class="cDetail">联系方式: {{ userDetail.telephone }}</div>
      <div class="cDetail">性别: {{ userDetail.sex == 0 ? '女' : '男' }}</div>
      <div class="cDetail">创建时间: {{ userDetail.createTime }}</div>
      <div class="cDetail">修改时间: {{ userDetail.editTime }}</div>
    </el-dialog>
  
  </div>
</template>

<script>
import api from "../network/home";

export default {
  data() {
    return {
      userForm: {},
      tableData: [],
      userDetail: {},
      showuserDetail: false,
      tableSize:0,
      pageNum:1,
    };
  },
  computed: {},
  methods: {
    pagechange(p){
      this.pageNum = p
      this.getUser()
    },
    //去到聊天页面
    onSubmit() {
      this.getUser()
    },
    resetForm(){
      this.userForm = {}
      this.getUser()
    },
    getUser(){
      const { name, idNo, telephone, recFlag } = this.userForm;
      const params = {
        pageNum: this.pageNum,
        pageSize: 10,
        name,
        idNo,
        telephone,
        recFlag,
      };
      api.getUser(params).then((res) => {
        this.tableData = res.data.data.records;
        this.tableSize = res.data.data.total
      });
    },
    changeFlag(id){
      const params = {
        userId: id
      };
      api.changeUserStatus(params).then(() => {
       this.getUser()
      });
    },
    goToDetail(id){
      api.userDetail(id).then((res) => {
       this.userDetail = res.data.data
       this.showuserDetail = true
      });
    }
  },
  mounted(){
    this.getUser()
  }
};
</script>

<style lang="scss" scoped>
.box-card{
  margin: 20px;
}
.cDetail{
  height: 50px;
  font-size: 20px;
  margin-left: 50px;
}
.itemDetail{
  margin-left: 50px;
  font-size: 18px;
  border: 1px solid ;
  width: 33%;
}
</style>